package com.example.toy.core.commit;

/**
 * CommitState
 *
 * @author shenbing
 * @date 2021/12/22
 */
public enum CommitState {
  REQUESTED,
  INFLIGHT,
  SUCCEED,
  FAILED;

  public static final CommitState[] SUCCEED_COMMIT_STATE = new CommitState[] {SUCCEED};
  public static final CommitState[] COMPLETED_COMMIT_STATE = new CommitState[] {SUCCEED, FAILED};
  public static final CommitState[] UN_COMPLETED_COMMIT_STATE =
      new CommitState[] {REQUESTED, INFLIGHT};

  public boolean isCompleted() {
    return this == SUCCEED || this == FAILED;
  }

  public boolean isRequested() {
    return this == REQUESTED;
  }

  public boolean isInflight() {
    return this == INFLIGHT;
  }

  public boolean supportTransform(CommitState state) {
    switch (this) {
      case REQUESTED:
        return state == INFLIGHT || state.isCompleted();
      case INFLIGHT:
        return state.isCompleted();
      case SUCCEED:
        return false;
      case FAILED:
        return false;
      default:
        throw new IllegalArgumentException("Never come here");
    }
  }
}
